package com.siwei.mes.sync.erp;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.sync.erp.ErpProjectService;
import com.siwei.mes.sync.erp.entity.SyncContract;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP合同
 *
 * @Description: ERP合同
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/8/14 16:34
 */
@Api(value = "合同信息")
@RestController
@RequestMapping("erp/contract")
public class SyncErpContractController {
    private static final Logger log = LoggerFactory.getLogger("erp");

    @Value("${erp-token}")
    private String ERP_TOKEN;
    @Resource
    private ErpProjectService erpProjectService;
    @Resource
    private ErpProjectMapper erpProjectMapper;


    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(ERP_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "新增或修改合同信息表")
    @PostMapping("/saveOrUpdate")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) SyncContract entity, HttpServletRequest request) {
        log.info("ERP新增或修改合同信息，入参：{}", JSON.toJSONString(entity));
        log.info("ERP新增或修改合同信息已禁用，直接在ERP新任务单的时候处理");
        //this.checkToken(request);
        //try {
        //    String projectIds = entity.getProjectIds();
        //    if (StringUtils.isNotBlank(projectIds)) {
        //        List<Integer> projectIdList = new ArrayList<>();
        //        /**如果这里传入的工程id不为空  这里的工程id是erp工程id*/
        //        for (String str : projectIds.split(",")) {
        //            projectIdList.add(Integer.valueOf(str));
        //        }
        //        List<ErpProject> projectList = erpProjectService.selectByErpIdList(projectIdList);
        //        if (projectList.isEmpty()) {
        //            assertTrue("没有查到对应的ERP工程信息，工程id：【" + projectIds + "】");
        //            return Json.succ();
        //        }
        //        for (ErpProject erpProject : projectList) {
        //            erpProject.setUpdateTime(new Date());
        //            erpProject.setErpContractId(entity.getCID());
        //            erpProject.setContractNo(entity.getContractNo());
        //            erpProject.setErpCustomId(entity.getCUSID());
        //            erpProject.setContractType(0);
        //            erpProjectMapper.updateByPrimaryKeySelective(erpProject);
        //        }
        //    }
        //} catch (Exception e) {
        //    log.info("ERP新增或修改合同信息失败，错误原因：【{}】", e.getMessage(), e);
        //    /**手动回滚事务*/
        //    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        //    assertTrue("ERP新增或修改合同信息失败，错误原因：" + e.getMessage() + "】");
        //}
        return Json.succ().data(new ApiResult());
    }
}
